package net.technics;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.models.Application;
import net.models.Etat;
import net.models.Fichefrais;
import net.models.Fraisforfait;
import net.models.Lignefraisforfait;
import net.models.Lignefraishorsforfait;
import net.models.Users;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class FicheVisiteEditer {
	private DataBindingContext		m_bindingContext;

	protected Shell					shellFicheVisiteEditer;
	private Text					txtAjouterUneFiche;
	private Text					textMois;
	private Text					textNbjustificatif;
	private List<Users>				users				= new ArrayList<Users>();
	private List<Fichefrais>		fichefraises		= new ArrayList<Fichefrais>();
	private List<Fraisforfait>		fraisforfaits		= new ArrayList<Fraisforfait>();
	private List<Etat>				etats				= new ArrayList<Etat>();
	private List<Lignefraisforfait>	Lignefraisforfaits	= new ArrayList<Lignefraisforfait>();
	private static List<Lignefraishorsforfait>	Lignefraishorsforfaits	= new ArrayList<Lignefraishorsforfait>();
	private ComboViewer				comboViewerFicheVisite;
	private Combo					combo;
	private CLabel					lbforfait1;
	private CLabel					lbforfait2;
	private CLabel					lbforfait3;
	private CLabel					lbforfait4;
	private Spinner					spqt1;
	private Spinner					spqt2;
	private Spinner					spqt3;
	private Spinner					spqt4;
	private Spinner					spqt5;
	private Spinner					spqt6;
	private Spinner					spqt8;
	private Spinner					spqt7;
	private Text					txt1;
	private Text					txt2;
	private Text					txt3;
	private Text					textdatemodif;

	private Spinner spfh1;
	private Spinner spfh2;
	private Spinner spfh3;
	private Text lb1;
	private Text lb2;
	private Text lb3;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					FicheVisiteEditer window = new FicheVisiteEditer();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		users = Application.getUsers();
		etats = Application.getEtats();
		Lignefraisforfaits = Application.getLignefraisforfaits();
		fichefraises = Application.getFichefraises();
		fraisforfaits = Application.getFraisforfaits();
		Lignefraishorsforfaits = Application.getLignefraishorsforfaits();
		Display display = Display.getDefault();
		createContents();
		shellFicheVisiteEditer.open();
		shellFicheVisiteEditer.layout();
		while (!shellFicheVisiteEditer.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shellFicheVisiteEditer = new Shell();
		shellFicheVisiteEditer.setSize(654, 565);
		shellFicheVisiteEditer.setText("SWT Application");
		shellFicheVisiteEditer.setLayout(null);

		Group group = new Group(shellFicheVisiteEditer, SWT.NONE);
		group.setEnabled(false);
		group.setText("Frais hors forfait");
		group.setBounds(188, 279, 439, 181);
		
		CLabel label = new CLabel(group, SWT.NONE);
		label.setText("");
		label.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		label.setBounds(70, 59, 70, 122);

		CLabel label_11 = new CLabel(group, SWT.NONE);
		label_11.setText("Montant");
		label_11.setBounds(10, 31, 76, 26);

		CLabel label_12 = new CLabel(group, SWT.NONE);
		label_12.setText("Libellé");
		label_12.setBounds(159, 31, 76, 26);

		CLabel label_13 = new CLabel(group, SWT.NONE);
		label_13.setText("Date");
		label_13.setBounds(317, 31, 76, 26);

		spfh1 = new Spinner(group, SWT.READ_ONLY);
		spfh1.setEnabled(true);
		spfh1.setMaximum(10000);
		spfh1.setBounds(10, 63, 81, 26);

		spfh2 = new Spinner(group, SWT.READ_ONLY);
		spfh2.setEnabled(true);
		spfh2.setMaximum(10000);
		spfh2.setBounds(10, 100, 81, 26);

		spfh3 = new Spinner(group, SWT.READ_ONLY);
		spfh3.setEnabled(true);
		spfh3.setMaximum(10000);
		spfh3.setBounds(10, 140, 81, 26);

		txt1 = new Text(group, SWT.READ_ONLY);
		txt1.setEnabled(true);
		txt1.setText("Aucune");
		txt1.setBounds(317, 63, 99, 26);

		txt2 = new Text(group, SWT.READ_ONLY);
		txt2.setEnabled(true);
		txt2.setText("Aucune");
		txt2.setBounds(317, 100, 99, 26);

		txt3 = new Text(group, SWT.READ_ONLY);
		txt3.setEnabled(true);
		txt3.setText("Aucune");
		txt3.setBounds(317, 140, 99, 26);
		
		lb1 = new Text(group, SWT.READ_ONLY);
		lb1.setEnabled(true);
		lb1.setText("Aucun");
		lb1.setBounds(156, 63, 126, 26);
		
		lb2 = new Text(group, SWT.READ_ONLY);
		lb2.setEnabled(true);
		lb2.setText("Aucun");
		lb2.setBounds(156, 100, 123, 26);
		
		lb3 = new Text(group, SWT.READ_ONLY);
		lb3.setEnabled(true);
		lb3.setText("Aucun");
		lb3.setBounds(156, 140, 123, 26);

		Group group_1 = new Group(shellFicheVisiteEditer, SWT.NONE);
		group_1.setText("Frais Forfait");
		group_1.setBounds(188, 70, 438, 203);

		spqt1 = new Spinner(group_1, SWT.BORDER | SWT.READ_ONLY);
		spqt1.setBounds(167, 59, 59, 26);

		spqt2 = new Spinner(group_1, SWT.BORDER | SWT.READ_ONLY);
		spqt2.setBounds(167, 91, 59, 26);

		spqt3 = new Spinner(group_1, SWT.BORDER | SWT.READ_ONLY);
		spqt3.setBounds(167, 123, 59, 26);

		spqt4 = new Spinner(group_1, SWT.BORDER | SWT.READ_ONLY);
		spqt4.setBounds(167, 155, 59, 26);

		lbforfait1 = new CLabel(group_1, SWT.NONE);
		lbforfait1.setEnabled(false);
		lbforfait1.setText("Aucun");
		lbforfait1.setBounds(10, 59, 141, 26);

		lbforfait2 = new CLabel(group_1, SWT.NONE);
		lbforfait2.setEnabled(false);
		lbforfait2.setText("Aucun");
		lbforfait2.setBounds(10, 91, 141, 26);

		lbforfait3 = new CLabel(group_1, SWT.NONE);
		lbforfait3.setEnabled(false);
		lbforfait3.setText("Aucun");
		lbforfait3.setBounds(10, 123, 141, 26);

		lbforfait4 = new CLabel(group_1, SWT.NONE);
		lbforfait4.setEnabled(false);
		lbforfait4.setText("Aucun");
		lbforfait4.setBounds(10, 155, 141, 26);

		CLabel label_8 = new CLabel(group_1, SWT.NONE);
		label_8.setText("Forfaits");
		label_8.setBounds(10, 27, 76, 26);

		CLabel label_9 = new CLabel(group_1, SWT.NONE);
		label_9.setText("Quantité");
		label_9.setBounds(150, 27, 76, 26);

		CLabel label_10 = new CLabel(group_1, SWT.NONE);
		label_10.setText("Total");
		label_10.setBounds(285, 27, 76, 26);

		CLabel lblNewLabel = new CLabel(group_1, SWT.NONE);
		lblNewLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		lblNewLabel.setBounds(340, 59, 76, 122);
		lblNewLabel.setText("");

		spqt8 = new Spinner(group_1, SWT.READ_ONLY);
		spqt8.setMaximum(2000);
		spqt8.setBounds(295, 155, 66, 26);

		spqt7 = new Spinner(group_1, SWT.READ_ONLY);
		spqt7.setMaximum(2000);
		spqt7.setBounds(295, 123, 66, 26);

		spqt6 = new Spinner(group_1, SWT.READ_ONLY);
		spqt6.setMaximum(2000);
		spqt6.setBounds(295, 91, 66, 26);

		spqt5 = new Spinner(group_1, SWT.READ_ONLY);
		spqt5.setMaximum(2000);
		spqt5.setBounds(295, 62, 66, 26);

		Composite composite_2 = new Composite(shellFicheVisiteEditer, SWT.NONE);
		composite_2.setBounds(376, 466, 251, 52);

		Button button = new Button(composite_2, SWT.NONE);
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shellFicheVisiteEditer.close();
			}
		});
		button.setText("Annuler");
		button.setBounds(131, 10, 90, 30);

		Button button_1 = new Button(composite_2, SWT.NONE);

		button_1.setText("Valider");
		button_1.setBounds(35, 10, 90, 30);

		Composite composite = new Composite(shellFicheVisiteEditer, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
		composite.setBounds(0, 0, 638, 48);

		txtAjouterUneFiche = new Text(composite, SWT.READ_ONLY);
		txtAjouterUneFiche.setTouchEnabled(true);
		txtAjouterUneFiche.setText("Consultation d'une fiche de visite d'un utilisateur");
		txtAjouterUneFiche.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		txtAjouterUneFiche.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD));
		txtAjouterUneFiche.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_RED));
		txtAjouterUneFiche.setBounds(10, 10, 428, 21);

		Group grpInformations = new Group(shellFicheVisiteEditer, SWT.NONE);
		grpInformations.setText("Sélection");
		grpInformations.setBounds(10, 70, 172, 389);

		CLabel lblVisiteur = new CLabel(grpInformations, SWT.NONE);
		lblVisiteur.setBounds(10, 30, 76, 26);
		lblVisiteur.setText("Visiteur");

		comboViewerFicheVisite = new ComboViewer(grpInformations, SWT.NONE);
		combo = comboViewerFicheVisite.getCombo();
		combo.setBounds(13, 62, 124, 28);

		CLabel lblMois = new CLabel(grpInformations, SWT.NONE);
		lblMois.setBounds(10, 96, 76, 26);
		lblMois.setText("Mois");

		textMois = new Text(grpInformations, SWT.BORDER);
		textMois.setBounds(13, 127, 124, 26);
		textMois.setEnabled(false);

		CLabel lblNbJustificatif = new CLabel(grpInformations, SWT.NONE);
		lblNbJustificatif.setBounds(10, 159, 127, 26);
		lblNbJustificatif.setText("Nb de justificatifs");

		textNbjustificatif = new Text(grpInformations, SWT.BORDER | SWT.READ_ONLY);
		textNbjustificatif.setBounds(10, 191, 127, 26);

		CLabel lblNewLabel_3 = new CLabel(grpInformations, SWT.NONE);
		lblNewLabel_3.setBounds(10, 223, 148, 26);
		lblNewLabel_3.setText("Date de modification");

		textdatemodif = new Text(grpInformations, SWT.NONE);
		textdatemodif.setBounds(10, 255, 127, 26);
		textdatemodif.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		combo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection sel = (IStructuredSelection) (comboViewerFicheVisite.getSelection());
				Users user = (Users) (sel.iterator().next());
				List<String> mois = new ArrayList<String>(Arrays.asList(" ","janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"));
				textMois.setText(" ");
				textNbjustificatif.setText(" ");
				lbforfait1.setText("Aucun");
				lbforfait2.setText("Aucun");
				lbforfait3.setText("Aucun");
				lbforfait4.setText("Aucun");

				spqt1.setSelection(0);
				spqt2.setSelection(0);
				spqt3.setSelection(0);
				spqt4.setSelection(0);

				spqt5.setSelection(0);
				spqt6.setSelection(0);
				spqt7.setSelection(0);
				spqt8.setSelection(0);
				
				spfh1.setSelection(0);
				spfh2.setSelection(0);
				spfh3.setSelection(0);
				
				lb1.setText("Aucun");
				lb2.setText("Aucun");
				lb3.setText("Aucun");
				
				txt1.setText("Aucune");
				txt2.setText("Aucune");
				txt3.setText("Aucune");
				for (Fichefrais ff : fichefraises) {
					if (ff.getUsers().getId().equals(user.getId())) {
						if (ff.getEtat().getId().equals("CR")) {
							int tt = 0;
							tt = Integer.parseInt(ff.getMois());
							textMois.setText(mois.get(tt));

							textNbjustificatif.setText(ff.getNbJustificatifs().toString());
							textdatemodif.setText(String.valueOf(ff.getDateModif()));
							
							List<Lignefraisforfait> lffs = new ArrayList<>();
							for(Lignefraisforfait lff : Lignefraisforfaits){
								if(lff.getFichefrais().getId().equals(ff.getId())){
									lffs.add(lff);
								}
							}
							
							if(lffs.size() == 1 ){
								lbforfait1.setText(fraisforfaits.get(0).getLibelle());
								spqt1.setSelection(Lignefraisforfaits.get(0).getQuantite());
								spqt5.setSelection(fraisforfaits.get(0).getMontant() * Lignefraisforfaits.get(0).getQuantite());						
							} else if(lffs.size() == 2){
								lbforfait1.setText(fraisforfaits.get(0).getLibelle());
								lbforfait2.setText(fraisforfaits.get(1).getLibelle());
								spqt1.setSelection(Lignefraisforfaits.get(0).getQuantite());
								spqt2.setSelection(Lignefraisforfaits.get(1).getQuantite());
								spqt5.setSelection(fraisforfaits.get(0).getMontant() * Lignefraisforfaits.get(0).getQuantite());
								spqt6.setSelection(fraisforfaits.get(1).getMontant() * Lignefraisforfaits.get(1).getQuantite());						
							} else if(lffs.size() == 3){
								lbforfait1.setText(fraisforfaits.get(0).getLibelle());
								lbforfait2.setText(fraisforfaits.get(1).getLibelle());
								lbforfait3.setText(fraisforfaits.get(2).getLibelle());
								spqt1.setSelection(Lignefraisforfaits.get(0).getQuantite());
								spqt2.setSelection(Lignefraisforfaits.get(1).getQuantite());
								spqt3.setSelection(Lignefraisforfaits.get(2).getQuantite());
								spqt5.setSelection(fraisforfaits.get(0).getMontant() * Lignefraisforfaits.get(0).getQuantite());
								spqt6.setSelection(fraisforfaits.get(1).getMontant() * Lignefraisforfaits.get(1).getQuantite());
								spqt7.setSelection(fraisforfaits.get(2).getMontant() * Lignefraisforfaits.get(2).getQuantite());								
							} else if(lffs.size() == 4){
								lbforfait1.setText(fraisforfaits.get(0).getLibelle());
								lbforfait2.setText(fraisforfaits.get(1).getLibelle());
								lbforfait3.setText(fraisforfaits.get(2).getLibelle());
								lbforfait4.setText(fraisforfaits.get(3).getLibelle());
								spqt1.setSelection(Lignefraisforfaits.get(0).getQuantite());
								spqt2.setSelection(Lignefraisforfaits.get(1).getQuantite());
								spqt3.setSelection(Lignefraisforfaits.get(2).getQuantite());
								spqt4.setSelection(Lignefraisforfaits.get(3).getQuantite());	
								spqt5.setSelection(fraisforfaits.get(0).getMontant() * Lignefraisforfaits.get(0).getQuantite());
								spqt6.setSelection(fraisforfaits.get(1).getMontant() * Lignefraisforfaits.get(1).getQuantite());
								spqt7.setSelection(fraisforfaits.get(2).getMontant() * Lignefraisforfaits.get(2).getQuantite());
								spqt8.setSelection(fraisforfaits.get(3).getMontant() * Lignefraisforfaits.get(3).getQuantite());							
							}
							
							List<Lignefraishorsforfait> lfhfs = new ArrayList<>();							
							for(Lignefraishorsforfait lfhf : Lignefraishorsforfaits){
								if(lfhf.getFichefrais().getId().equals(ff.getId())){
									lfhfs.add(lfhf);
								}
							}
							
							if(lfhfs.size() == 1){
								spfh1.setSelection(Lignefraishorsforfaits.get(0).getMontant());
								lb1.setText(Lignefraishorsforfaits.get(0).getLibelle());
								txt1.setText(String.valueOf(Lignefraishorsforfaits.get(0).getDateHf()));								
							} else if(lfhfs.size() == 2){
								spfh1.setSelection(Lignefraishorsforfaits.get(0).getMontant());
								spfh2.setSelection(Lignefraishorsforfaits.get(1).getMontant());		
								lb1.setText(Lignefraishorsforfaits.get(0).getLibelle());
								lb2.setText(Lignefraishorsforfaits.get(1).getLibelle());	
								txt1.setText(String.valueOf(Lignefraishorsforfaits.get(0).getDateHf()));
								txt2.setText(String.valueOf(Lignefraishorsforfaits.get(1).getDateHf()));					
							} else if(lfhfs.size() == 3){
									spfh1.setSelection(Lignefraishorsforfaits.get(0).getMontant());
									spfh2.setSelection(Lignefraishorsforfaits.get(1).getMontant());
									spfh3.setSelection(Lignefraishorsforfaits.get(2).getMontant());
									lb1.setText(Lignefraishorsforfaits.get(0).getLibelle());
									lb2.setText(Lignefraishorsforfaits.get(1).getLibelle());
									lb3.setText(Lignefraishorsforfaits.get(2).getLibelle());
									txt1.setText(String.valueOf(Lignefraishorsforfaits.get(0).getDateHf()));
									txt2.setText(String.valueOf(Lignefraishorsforfaits.get(1).getDateHf()));
									txt3.setText(String.valueOf(Lignefraishorsforfaits.get(2).getDateHf()));
								}
						}
					}
				}
			}
		});
		m_bindingContext = initDataBindings();

	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap[] observeMaps = BeansObservables.observeMaps(listContentProvider.getKnownElements(), Users.class, new String[] { "name", "prenom" });
		comboViewerFicheVisite.setLabelProvider(new ObservableMapLabelProvider(observeMaps));
		comboViewerFicheVisite.setContentProvider(listContentProvider);
		//
		IObservableList selfList = Properties.selfList(Users.class).observe(users);
		comboViewerFicheVisite.setInput(selfList);
		//
		return bindingContext;
	}
}
